//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.amado.springboot.springbootcms.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class RedisTemplateDriver {
	private Map<String, RedisAtomicInteger> redisAtomicIntegerMapping;
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	public RedisTemplateDriver() {
		this.redisAtomicIntegerMapping = new HashMap();
	}
	public void set(String key, Object value, long expire) {
		this.redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
	}
	public List<Object> mGet(Collection<Object> keys) {
		return this.redisTemplate.opsForValue().multiGet(keys);
	}
	public void mset(Map<String, Object> map) {
		this.redisTemplate.opsForValue().multiSet(map);
	}
	public void set(String key, Object value) {
		this.redisTemplate.opsForValue().set(key, value);
	}
	public boolean expireForHours(String key, int hours) {
		return this.expireForMinute(key, hours * 60);
	}
	public boolean expireForMinute(String key, int minute) {
		return this.redisTemplate.expire(key, (long) minute, TimeUnit.MINUTES).booleanValue();
	}
	public Object getObj(String key) {
		DataType valueType = this.redisTemplate.type(key);
		if (DataType.STRING == valueType) {
			return this.redisTemplate.opsForValue().get(key);
		} else if (DataType.SET == valueType) {
			return this.redisTemplate.opsForSet().pop(key);
		} else {
			Long size;
			if (DataType.LIST == valueType) {
				ListOperations<Object, Object> list = this.redisTemplate.opsForList();
				size = list.size(key);
				if (size != null) {
					return list.range(key, 0L, size.longValue());
				}
			} else {
				if (DataType.HASH == valueType) {
					return this.redisTemplate.opsForHash().entries(key);
				}
				if (DataType.ZSET == valueType) {
					ZSetOperations<Object, Object> zSet = this.redisTemplate.opsForZSet();
					size = zSet.size(key);
					if (size != null) {
						return zSet.range(key, 0L, size.longValue());
					}
				}
			}
			return null;
		}
	}
	public void del(String... key) {
		this.redisTemplate.delete(Arrays.asList(key));
	}
	public void del(List<Object> keys) {
		this.redisTemplate.delete(keys);
	}
	public void del(String key) {
		this.redisTemplate.delete(key);
	}
	public void delpn(String... pattern) {
		String[] var2 = pattern;
		int var3 = pattern.length;
		for (int var4 = 0; var4 < var3; ++var4) {
			String kp = var2[var4];
			this.redisTemplate.delete(this.redisTemplate.keys(kp + "*"));
		}
	}
	public boolean exists(String key) {
		return this.redisTemplate.hasKey(key).booleanValue();
	}
	public Integer auomicInteger(String key) {
		if (this.redisAtomicIntegerMapping.get(key) == null) {
			this.redisAtomicIntegerMapping.put(key, new RedisAtomicInteger(key, this.getConn()));
		}
		return Integer.valueOf(((RedisAtomicInteger) this.redisAtomicIntegerMapping.get(key)).incrementAndGet());
	}
	public Integer getAuomicInteger(String key) {
		if (!this.exists(key)) {
			this.redisAtomicIntegerMapping.remove(key);
		} else {
			this.redisAtomicIntegerMapping.put(key, new RedisAtomicInteger(key, this.getConn()));
		}
		return this.redisAtomicIntegerMapping.get(key) != null
				? Integer.valueOf(((RedisAtomicInteger) this.redisAtomicIntegerMapping.get(key)).get())
				: null;
	}
	public RedisConnectionFactory getConn() {
		return this.redisTemplate.getConnectionFactory();
	}
	public boolean zadd(String key, Object value, double score) {
		return key == null ? false : this.redisTemplate.opsForZSet().add(key, value, score).booleanValue();
	}
	public Double zscore(String key, Object member) {
		if (key == null) {
			return Double.valueOf(0.0D);
		} else {
			Double score = this.redisTemplate.opsForZSet().score(key, member);
			return Double.valueOf(score == null ? 0.0D : score.doubleValue());
		}
	}
	public Long zrank(String key, Object member) {
		return StringUtils.isEmpty(key) ? null : this.redisTemplate.opsForZSet().rank(key, member);
	}
	public long zrem(String key, Object value) {
		return key == null ? 0L : this.redisTemplate.opsForZSet().remove(key, new Object[] { value }).longValue();
	}
	public double incrementScore(String key, Object value, double delta) {
		return key == null ? 0.0D : this.redisTemplate.opsForZSet().incrementScore(key, value, delta).doubleValue();
	}
	public Set<TypedTuple<Object>> rangeForScore(String key, long start, long end) {
		if (StringUtils.isEmpty(key)) {
			return null;
		} else {
			Set<TypedTuple<Object>> set = this.redisTemplate.opsForZSet().rangeWithScores(key, start, end);
			return set;
		}
	}
	public Set<Object> keys(Object pattern) {
		return this.redisTemplate.keys(pattern);
	}
	public void hashPutAll(String name, Map<Object, Object> map) {
		this.redisTemplate.opsForHash().putAll(name, map);
	}
	public void hashPut(String name, String key, Object value) {
		this.redisTemplate.opsForHash().put(name, key, value);
	}
	public Object hashGet(String name, Object key) {
		return this.redisTemplate.opsForHash().get(name, key);
	}
	public void hashDelete(String name, Object... key) {
		this.redisTemplate.opsForHash().delete(name, key);
	}
	public Long rpush(String key, Object... values) {
		return this.redisTemplate.opsForList().rightPushAll(key, values);
	}
	public Long rpush(String key, Object value) {
		return this.redisTemplate.opsForList().rightPush(key, value);
	}
	public long llen(String key) {
		return StringUtils.isEmpty(key) ? -1L : this.redisTemplate.opsForList().size(key).longValue();
	}
	public List<Object> lrange(String key, long start, long end) {
		return this.redisTemplate.opsForList().range(key, start, end);
	}
	public Long sadd(String key, Object... value) {
		return key == null ? Long.valueOf(0L) : this.redisTemplate.opsForSet().add(key, value);
	}
	public Set<Object> smembers(String key) {
		return this.redisTemplate.opsForSet().members(key);
	}
	public Object getRandomMember(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		} else {
			List<Object> rs = this.redisTemplate.opsForSet().randomMembers(key, 1L);
			return rs != null && rs.size() != 0 ? rs.get(0) : null;
		}
	}
	public Long delForSet(String key, Object... values) {
		return StringUtils.isEmpty(key) ? Long.valueOf("-1") : this.redisTemplate.opsForSet().remove(key, values);
	}
}
